package wubj.com.tendencydemo.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import wubj.com.tendencydemo.R;


/**
 * Wubj 创建于 2017/1/10 0027.
 */
public class DateRangeDialog extends Dialog {

    private TextView leftTv;
    private TextView rightTv;
    private View.OnClickListener leftListener;
    private View.OnClickListener rightListener;
    private Context mContext;

    private DateRangeDialog(Context context, int theme) {
        super(context, theme);
        getWindow().setGravity(Gravity.TOP);
//        WindowManager m = getWindow().getWindowManager();
//        Display d = m.getDefaultDisplay();
//        WindowManager.LayoutParams p = getWindow().getAttributes();
//        p.width = d.getWidth();
//        getWindow().setAttributes(p);
        mContext = context;
        this.setContentView(R.layout.dialog_date_range);
        this.setCanceledOnTouchOutside(false);
        findView();
    }

    private void findView() {
        leftTv = (TextView) findViewById(R.id.txt_left);
        rightTv = (TextView) findViewById(R.id.txt_right);
    }

    public void setLeftListener(View.OnClickListener listener) {
        this.leftListener = listener;
    }

    public void setRightListener(View.OnClickListener listener) {
        this.rightListener = listener;
    }

    public void setListener() {
        leftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftListener != null) {
                    leftListener.onClick(v);
                }
                dismiss();
            }
        });
        rightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rightListener != null) {
                    rightListener.onClick(v);
                }
                dismiss();
            }
        });
    }

    public void setLeftText(String message) {
        if (!TextUtils.isEmpty(message)) {
            leftTv.setText(message);
        }
    }

    public void setRightText(String message) {
        if (!TextUtils.isEmpty(message)) {
            rightTv.setText(message);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            dismiss();
        }
    }

    public static class Builder {
        DateRangeDialog mEditTextDialog;

        public Builder(Context context, int theme) {
            mEditTextDialog = new DateRangeDialog(context, theme);
        }

        public Builder(Context context) {
            this(context, R.style.CustomDialog);
        }

        public DateRangeDialog build() {
            return mEditTextDialog;
        }
    }


}
